home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / boot / czesc_2 / toolmanager / source / prefs / config.c < prev    next >
C/C++ Source or Header  |  1993-05-15  |  7KB  |  280 lines

  1. /*
  2.  * config.c  V2.1
  3.  *
  4.  * preferences file handling
  5.  *
  6.  * (c) 1990-1993 Stefan Becker
  7.  */
  8.  
  9. #include "ToolManagerConf.h"
  10.  
  11. /* misc. data */
  12. #define BUFSIZE 65536
  13. ULONG stopchunks[]={ID_PREF,ID_TMEX,
  14.                     ID_PREF,ID_TMIM,
  15.                     ID_PREF,ID_TMSO,
  16.                     ID_PREF,ID_TMMO,
  17.                     ID_PREF,ID_TMIC,
  18.                     ID_PREF,ID_TMDO,
  19.                     ID_PREF,ID_TMAC};
  20. struct PrefHeader PrefHdrChunk={TMPREFSVERSION,0,0};
  21.  
  22. /* Function tables */
  23. ReadNodeFuncPtr ReadNodeFunctions[TMOBJTYPES]={
  24.                                                ReadExecNode,
  25.                                                ReadImageNode,
  26.                                                ReadSoundNode,
  27.                                                ReadMenuNode,
  28.                                                ReadIconNode,
  29.                                                ReadDockNode,
  30.                                                ReadAccessNode
  31.                                               };
  32.  
  33. WriteNodeFuncPtr WriteNodeFunctions[TMOBJTYPES]={
  34.                                                  WriteExecNode,
  35.                                                  WriteImageNode,
  36.                                                  WriteSoundNode,
  37.                                                  WriteMenuNode,
  38.                                                  WriteIconNode,
  39.                                                  WriteDockNode,
  40.                                                  WriteAccessNode
  41.                                                 };
  42.  
  43. /* Read config file */
  44. BOOL ReadConfigFile(char *filename)
  45. {
  46.  UBYTE *configbuf;
  47.  BOOL rc=FALSE;
  48.  
  49.  DEBUG_PRINTF("read config from '%s'\n",filename);
  50.  
  51.  /* Allocate memory for config buffer */
  52.  if (configbuf=malloc(BUFSIZE)) {
  53.   struct IFFHandle *iff;
  54.  
  55.   DEBUG_PRINTF("config buffer: 0x%08lx\n",configbuf)
  56.  
  57.   /* Allocate IFF handle */
  58.   if (iff=AllocIFF()) {
  59.    /* Every error will be ignored after this point! */
  60.    rc=TRUE;
  61.  
  62.    DEBUG_PRINTF("IFF Handle: 0x%08lx\n",iff);
  63.  
  64.    /* Open IFF File */
  65.    if (iff->iff_Stream=Open(filename,MODE_OLDFILE)) {
  66.     /* Init IFF handle */
  67.     InitIFFasDOS(iff);
  68.  
  69.     DEBUG_PRINTF("IFF Stream: 0x%08lx\n",iff->iff_Stream);
  70.  
  71.     /* Open IFF handle */
  72.     if (!OpenIFF(iff,IFFF_READ)) {
  73.  
  74.      DEBUG_PRINTF("IFF open\n");
  75.  
  76.      /* Start IFF parsing */
  77.      if (!ParseIFF(iff,IFFPARSE_STEP)) {
  78.       struct ContextNode *cn;
  79.  
  80.       DEBUG_PRINTF("First IFF scan step\n");
  81.  
  82.       /* Check IFF type and set IFF chunk types */
  83.       if ((cn=CurrentChunk(iff)) && (cn->cn_ID==ID_FORM) &&
  84.           (cn->cn_Type==ID_PREF) &&
  85.           !PropChunk(iff,ID_PREF,ID_PRHD) &&
  86.           !StopChunks(iff,stopchunks,TMOBJTYPES) &&
  87.           !StopOnExit(iff,ID_PREF,ID_FORM) &&
  88.           !ParseIFF(iff,IFFPARSE_SCAN)) {
  89.        /* First stop chunk encountered */
  90.        struct StoredProperty *sp;
  91.  
  92.        /* Get pointer to PRHD chunk */
  93.        if (sp=FindProp(iff,ID_PREF,ID_PRHD)) {
  94.         struct PrefHeader *ph=(struct PrefHeader *) sp->sp_Data;
  95.  
  96.         /* Check file version number */
  97.         if (ph->ph_Version==TMPREFSVERSION) {
  98.  
  99.          /* Parse IFF chunks */
  100.          do {
  101.           /* Get current chunk */
  102.           if (cn=CurrentChunk(iff)) {
  103.            LONG type;
  104.  
  105.            DEBUG_PRINTF("chunk ID: 0x%08lx",cn->cn_ID);
  106.            DEBUG_PRINTF(" size: 0x%08lx",cn->cn_Size);
  107.  
  108.            /* Read IFF chunk according to chunk ID */
  109.            switch(cn->cn_ID) {
  110.             case ID_TMEX: type=TMOBJTYPE_EXEC;
  111.                           break;
  112.             case ID_TMIM: type=TMOBJTYPE_IMAGE;
  113.                           break;
  114.             case ID_TMSO: type=TMOBJTYPE_SOUND;
  115.                           break;
  116.             case ID_TMMO: type=TMOBJTYPE_MENU;
  117.                           break;
  118.             case ID_TMIC: type=TMOBJTYPE_ICON;
  119.                           break;
  120.             case ID_TMDO: type=TMOBJTYPE_DOCK;
  121.                           break;
  122.             case ID_TMAC: type=TMOBJTYPE_ACCESS;
  123.                           break;
  124.             default:      type=-1;
  125.                           break;
  126.            }
  127.  
  128.            DEBUG_PRINTF(" type: %ld\n",type);
  129.  
  130.            /* valid type? */
  131.            if (type!=-1) {
  132.             ULONG size=cn->cn_Size;
  133.  
  134.             /* Read chunk */
  135.             if (ReadChunkBytes(iff,configbuf,size)==size) {
  136.              struct Node *node;
  137.  
  138.              DEBUG_PRINTF("chunk read\n");
  139.  
  140.              /* Interpret chunk contents */
  141.              if (node=(*ReadNodeFunctions[type])(configbuf)) {
  142.  
  143.               DEBUG_PRINTF("new node: 0x%08lx\n",node);
  144.  
  145.               /* Make sure ln_Name is valid */
  146.               if (!node->ln_Name) node->ln_Name=strdup("");
  147.  
  148.               /* Append new node to list */
  149.               AddTail(&ObjectLists[type],node);
  150.              }
  151.             }
  152.            }
  153.           }
  154.          /* Next parse step */
  155.          } while (!ParseIFF(iff,IFFPARSE_SCAN));
  156.         }
  157.        }
  158.       }
  159.      }
  160.      CloseIFF(iff);
  161.     }
  162.     Close(iff->iff_Stream);
  163.    }
  164.    FreeIFF(iff);
  165.   }
  166.   free(configbuf);
  167.  }
  168.  return(rc);
  169. }
  170.  
  171. /* Write config file */
  172. BOOL WriteConfigFile(char *filename)
  173. {
  174.  UBYTE *configbuf;
  175.  BOOL rc=FALSE;
  176.  
  177.  DEBUG_PRINTF("write config to '%s'\n",filename);
  178.  
  179.  /* Allocate memory for config buffer */
  180.  if (configbuf=malloc(BUFSIZE)) {
  181.   struct IFFHandle *iff;
  182.  
  183.   DEBUG_PRINTF("config buffer: 0x%08lx\n",configbuf)
  184.  
  185.   /* Allocate IFF handle */
  186.   if (iff=AllocIFF()) {
  187.  
  188.    DEBUG_PRINTF("IFF Handle: 0x%08lx\n",iff);
  189.  
  190.    /* Open IFF File */
  191.    if (iff->iff_Stream=Open(filename,MODE_NEWFILE)) {
  192.     /* Init IFF handle */
  193.     InitIFFasDOS(iff);
  194.  
  195.     DEBUG_PRINTF("IFF Stream: 0x%08lx\n",iff->iff_Stream);
  196.  
  197.     /* Open IFF handle */
  198.     if (!OpenIFF(iff,IFFF_WRITE)) {
  199.  
  200.      DEBUG_PRINTF("IFF open\n");
  201.  
  202.      /* Push FORM IFF chunk */
  203.      if (!PushChunk(iff,ID_PREF,ID_FORM,IFFSIZE_UNKNOWN)) {
  204.  
  205.       /* Write PRHD IFF chunk */
  206.       if (!PushChunk(iff,0,ID_PRHD,sizeof(struct PrefHeader)) &&
  207.           (WriteChunkBytes(iff,(UBYTE *) &PrefHdrChunk,
  208.                            sizeof(struct PrefHeader))
  209.                             ==sizeof(struct PrefHeader)) &&
  210.           !PopChunk(iff))
  211.        /* Set return code */
  212.        rc=TRUE;
  213.  
  214.       /* error? */
  215.       if (rc) {
  216.        ULONG i;
  217.  
  218.        /* No, scan all object lists */
  219.        for (i=0; (i<TMOBJTYPES) && rc; i++) {
  220.         struct Node *node=GetHead(&ObjectLists[i]);
  221.         WriteNodeFuncPtr wnfp=WriteNodeFunctions[i];
  222.  
  223.         /* Scan list */
  224.         while (node && rc) {
  225.          /* Convert node into IFF chunk */
  226.          rc=(*wnfp)(iff,configbuf,node);
  227.  
  228.          /* Get next node */
  229.          node=GetSucc(node);
  230.         }
  231.        }
  232.       }
  233.      }
  234.      CloseIFF(iff);
  235.     }
  236.     Close(iff->iff_Stream);
  237.  
  238.     /* Clear execution flag */
  239.     SetProtection(filename,FIBF_EXECUTE);
  240.    }
  241.    FreeIFF(iff);
  242.   }
  243.   free(configbuf);
  244.  }
  245.  return(rc);
  246. }
  247.  
  248. /* Read one config string and correct pointer */
  249. char *GetConfigStr(UBYTE **buf)
  250. {
  251.  char *s=*buf;
  252.  char *new;
  253.  ULONG len=strlen(s)+1;
  254.  
  255.  /* Allocate string buffer */
  256.  if (new=malloc(len)) {
  257.   /* Copy string */
  258.   strcpy(new,s);
  259.  
  260.   /* Correct pointer */
  261.   *buf+=len;
  262.  }
  263.  return(new);
  264. }
  265.  
  266. /* Write one config string and correct pointer */
  267. BOOL PutConfigStr(char *s, UBYTE **buf)
  268. {
  269.  /* string valid? */
  270.  if (s) {
  271.   /* Copy string to buffer */
  272.   strcpy(*buf,s);
  273.  
  274.   /* Correct pointer */
  275.   *buf+=strlen(s)+1;
  276.   return(TRUE);
  277.  }
  278.  return(FALSE);
  279. }
  280.